package main

import (
	"fmt"
	"log"
	"net/http"
	"strconv"
	"strings"
)

// Server1 is a minimal "echo" server.
func main() {
	http.HandleFunc("/", handler) // each request calls handler
	log.Fatal(http.ListenAndServe("localhost:8000", nil))
}

// handler echoes the Path component of the request URL r.
func handler(w http.ResponseWriter, r *http.Request) {
	data := getData(r.URL.RawQuery, "code")
	a, _ := strconv.Atoi(data)
	_, err := fmt.Fprintf(w, "URL.Path = %q\nCode = %d\n", r.URL.Path, a+20)
	if err != nil {
		return
	}
}

func getData(rawQuery, str string) string {
	for _, s := range strings.Split(rawQuery, "&") {
		prefix := str + "="
		if strings.HasPrefix(s, prefix) {
			return strings.ReplaceAll(s, prefix, "")
		}
	}
	return ""
}
